package cn.lau.demo.controller;

import cn.lau.demo.pojo.User;
import cn.lau.demo.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/u/")
public class UserController {

	@Autowired
	private UserService userService;

	@PostMapping("login")
	public Map<String, Object> login(@RequestBody User user) {
		return userService.login(user.getUsername().trim(), user.getPassword().trim());
	}

	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@RequestMapping("notLogin")
	public Map<String, Object> notLogin() {
		Map<String, Object> result = new HashMap<>();
		result.put("code", 1);
		result.put("msg", "未登录");
		result.put("data", null);
		return result;
	}

	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@RequestMapping("unauthorized")
	public Map<String, Object> unauthorized() {
		Map<String, Object> result = new HashMap<>();
		result.put("code", 1);
		result.put("msg", "无权访问");
		result.put("data", null);
		return result;
	}

}
